<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/18
 * Time: 11:03
 */
namespace app\index\controller;
use think\Db;
use think\Request;

class Settlement extends Common{
	
	//判断是否登录
	public function _initialize()
	{
		parent::_initialize();
	}
	
	/**
	 * 商家列表
	 * @param string get.title 要检索的字段
	 * @return Array result 查询结果
	*/
	public function index()
	{
		$where = ['query'=>[]];
		$db = Db::table('business')
			->field('business.id,business.name,business.linkman,business.telephone,
					business.paymentaccount,business.bank,business.branch,business.account,business.email')
			->join('purchase','purchase.bid = business.id','LEFT')
			->order('business.id desc');
		if(!empty(trim(input('get.title')))){
			$title = trim(input('get.title'));
			$db->where('business.account|business.linkman|business.telephone','like',"%{$title}%");
			$where['query']['title'] = $title;
		}
		$db->whereIn('purchase.status',[1,2,3])->group('business.email');
		$result = $db->paginate(8,false,$where);
		return view('index',compact('result'));
		
	}
	
	/**
	 * 商家订单
	 * Method Get
	 * @param string get.id 商家id
	 * @param string get.title 要检索的字段
	 * @param string get.status 查询状态
	 * @return Array result 查询结果
	 * @return string id 商家id
	 * @return string title 要检索的字段
	 * @return string status 状态
	*/
	public function order()
	{
		$db = Db::table('purchase')
			->field('business.linkman,business.telephone,purchase.number,purchase.describe,purchase.total,
					purchase.actualtotal,purchase.date,purchase.ordernumber,purchase.id,purchase.status,purchase.pmethod,
					product.pname,product.purchaseprice,product.retailprice,shop.address')
			->join('business','purchase.bid = business.id','LEFT')
			->join('product','purchase.pid = product.id','LEFT')
			->join('shop','purchase.sid = shop.id','LEFT')
			->where('business.id',input('get.id'))
			//->where('purchase.status',4)
			->order('purchase.id desc')
			->group('purchase.ordernumber');
		if(empty(trim(input('get.status')))){
			$db->whereIn('purchase.status',[1,2,3,4,5]);
			$status = 0;
		}
		//未付款
		if(trim(input('get.status')) == 4){
			$db->whereIn('purchase.status',[1,2]);
			$status = trim(input('get.status'));
		}
		//以付款
		if(trim(input('get.status')) == 5){
			$db->whereIn('purchase.status',[3,4,5]);
			$status = trim(input('get.status'));
		}
		$where = ['query'=>[]];
		$where['query']['status'] = trim(input('get.status'));
		$id = trim(input('get.id'));
		$where['query']['id'] = $id;
		if(!empty(trim(input('get.title')))){
			$title = trim(input('get.title'));
			$db->where('product.pname|business.linkman|shop.address|purchase.ordernumber','like',"%{$title}%");
			$where['query']['title'] = $title;
		}
		$result = $db->paginate(8,false,$where);
		return view('order',compact('result','id','title','status'));
	}
	/**
	 * 订单详情
	 * Method get
	 * @param string get.ordernumber 订单号
	 * @param string get.bid 商家id
	 * @return Array result 查询结果
	*/
	public function detail()
	{
		$where = ['query'=>[]];
		$ordernumber = trim(input('get.ordernumber'));
		$bid = trim(input('get.bid'));
		$db = Db::table('purchase')
			->field('business.linkman,business.telephone,purchase.number,purchase.describe,purchase.total,
					purchase.actualtotal,purchase.date,purchase.ordernumber,purchase.id,purchase.status,purchase.pmethod,
					product.pname,product.purchaseprice,product.retailprice,shop.address')
			->join('business','purchase.bid = business.id','LEFT')
			->join('product','purchase.pid = product.id','LEFT')
			->join('shop','purchase.sid = shop.id','LEFT')
			->where('business.id',$bid)
			->where('purchase.status','<>',0);
		$where['query']['ordernumber'] = $ordernumber;
		$where['query']['bid'] = $bid;
		$result = $db->where('purchase.ordernumber',$ordernumber)
				->order('purchase.id desc')
				->paginate(8,false,$where);
		return view('detail',compact('result','bid','ordernumber'));
	}
	/**
	 * @param string get.id 订单id
	 * @param string get.id 商家id
	 * @return Array result 查询结果
	*/
	public function orderDetail()
	{
		$id = trim(input('get.id'));
		$bid = trim(input('get.bid'));
		$result = Db::table('purchase')
				->join('business','purchase.bid = business.id','LEFT')
				->join('product','purchase.pid = product.id','LEFT')
				->join('shop','purchase.sid = shop.id','LEFT')
				->where('business.id',$bid)
				->where('purchase.id',$id)
				->find();
		$result['image'] = json_decode($result['image']);
		return view('orderDetail',compact('result'));
	}
	/**
	 * 订单删除
	 * Method Post
	 * @param string post.id 订单id
	 * @return json success 删除成功 error 删除失败的错误信息
	*/
	public function del()
	{
		if(Request()->isAjax()){
			try{
				Db::startTrans();
				$id = input('post.id');
				$result = Db::table('purchase')->where('id',$id)->update(['status'=>0]);
				if(!$result){
					throw new \PDOException('删除失败');
				}
				Db::commit();
				$success = ['success' => '删除成功'];
				return json($success);
			}catch (\PDOException $e){
				Db::rollback();
				$error = ['eroro'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	/**
	 * 加载图片添加页面
	*/
	public function image()
	{
		$id = input('get.id');
		return view('image',compact('id'));
	}
	/**
	 * 打款操作
	 * Method Post
	 * @param string post.id  订单号
	 * @param Array image 支付凭证
	 * @return json success 操作成功  error 操作失败错误信息
	*/
	public function money()
	{
		if(Request()->isAjax()){
			try{
				$id = input('get.id');
				if(!$id){
					throw new \PDOException('订单异常请联系管理员');
				}
				if(request()->file('file')){
					$file = request()->file('file')	;
					$path =  ROOT_PATH . 'public' . DS . 'purchase' . DS . $id . DS .'images';
					$imgPath = 'purchase' . DS . $id . DS .'images';
					$info = $file->validate(['size'=>1567800])->move($path);
					if($info){
						$image = $imgPath . DS . $info->getSaveName();
					}else{
						throw new \PDOException($file->getError());
					}
				}else{
					throw new \PDOException('请添加支付凭证');
				}
				Db::startTrans();
				$result = Db::table('purchase')->where('id',$id)->update(['images'=>$image]);
				if(!$result){
					throw new \PDOException('结算操作失败');
				}
				Db::commit();
				$success = ['success'=>'打款成功'];
				return json($success);
			}catch (\PDOException $e){
				Db::rollback();
				$error = ['error'=>$e->getMessage()];
				return join($error);
			}
		}
	}
}
